/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.asn1.formatting;

import static de.thwildau.tm.moses.asn1.util.Asn1Keywords.CLOSING_BRACKET;
import static de.thwildau.tm.moses.asn1.util.Asn1Keywords.OPENING_BRACKET;
import static de.thwildau.tm.moses.asn1.util.Asn1Keywords.OPENING_CURLED_BRACKET;

import java.util.List;

import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.formatting.impl.AbstractDeclarativeFormatter;
import org.eclipse.xtext.formatting.impl.FormattingConfig;
import org.eclipse.xtext.util.Pair;

import de.thwildau.tm.moses.asn1.services.Asn1GrammarAccess;

/**
 * This class contains custom formatting description.
 * 
 * @author Thomas Kistel
 * @version $Revision: 763 $
 * @see http://www.eclipse.org/Xtext/documentation/latest/xtext.html#formatting
 */
public class Asn1Formatter extends AbstractDeclarativeFormatter {

	@Override
	protected void configureFormatting(FormattingConfig c) {
		Asn1GrammarAccess g = (Asn1GrammarAccess) getGrammarAccess();
		// It's usually a good idea to activate the following three statements.
		// They will add and preserve newlines around comments
		c.setLinewrap(0, 1, 2).before(g.getSL_COMMENTRule());
		c.setLinewrap(0, 1, 2).after(g.getSL_COMMENTRule());
		c.setLinewrap(0, 1, 2).before(g.getML_COMMENTRule());
		c.setLinewrap(0, 1, 1).after(g.getML_COMMENTRule());

		// Linebreak before a Definition
		c.setLinewrap(1, 1, 2).before(g.getModuleDefinitionRule());
		// Linebreak for BEGIN and increment indentation
		c.setLinewrap(2).after(
				g.getModuleDefinitionAccess().getBEGINKeyword_6());
		c.setIndentationIncrement().after(
				g.getModuleDefinitionAccess().getBEGINKeyword_6());
		// Linebreak before END and decrement indentation
		c.setIndentationDecrement().before(
				g.getModuleDefinitionAccess().getENDKeyword_8());
		c.setLinewrap(1)
				.before(g.getModuleDefinitionAccess().getENDKeyword_8());
		// Imports
		c.setLinewrap().around(g.getImportsRule());
		// Exports
		c.setLinewrap().around(g.getExportsRule());
		// Linebreak before a TypeAssignement
		c.setLinewrap(1, 1, 2).before(g.getTypeAssignmentRule());

		// remove spaces before ',' and ';'
		c.setNoSpace().before(g.getExportsAccess().getSemicolonKeyword_3());
		c.setNoLinewrap().before(g.getExportsAccess().getSemicolonKeyword_3());
		c.setNoSpace().before(g.getExportsAccess().getCommaKeyword_2_0_1_0());
		c.setNoLinewrap()
				.before(g.getExportsAccess().getCommaKeyword_2_0_1_0());
		c.setNoSpace().before(
				g.getComponentTypeListAccess().getCommaKeyword_1_0());
		c.setNoLinewrap().before(
				g.getComponentTypeListAccess().getCommaKeyword_1_0());
		c.setNoSpace()
				.before(g.getEnumeratedTypeAccess().getCommaKeyword_5_0());
		c.setNoLinewrap().before(
				g.getEnumeratedTypeAccess().getCommaKeyword_5_0());
		c.setNoSpace().before(g.getSequenceTypeAccess().getCommaKeyword_4());
		c.setNoLinewrap().before(g.getSequenceTypeAccess().getCommaKeyword_4());
		c.setNoSpace().before(
				g.getComponentValueListAccess().getCommaKeyword_1_0());
		c.setNoLinewrap().before(
				g.getComponentValueListAccess().getCommaKeyword_1_0());
		c.setNoSpace().before(g.getChoiceTypeAccess().getCommaKeyword_4_0());
		c.setNoLinewrap().before(g.getChoiceTypeAccess().getCommaKeyword_4_0());
		c.setNoSpace().before(
				g.getElementSetSpecsAccess().getCommaKeyword_1_0());
		c.setNoLinewrap().before(
				g.getElementSetSpecsAccess().getCommaKeyword_1_0());
		c.setNoSpace().before(
				g.getElementSetSpecsAccess().getCommaKeyword_1_2_0());
		c.setNoLinewrap().before(
				g.getElementSetSpecsAccess().getCommaKeyword_1_2_0());
		c.setNoSpace().before(g.getIntegerTypeAccess().getCommaKeyword_2_2_0());
		c.setNoLinewrap().before(
				g.getIntegerTypeAccess().getCommaKeyword_2_2_0());
		c.setNoSpace().before(
				g.getRealSequenceValueAccess().getCommaKeyword_3());
		c.setNoLinewrap().before(
				g.getRealSequenceValueAccess().getCommaKeyword_3());
		c.setNoSpace().before(
				g.getRealSequenceValueAccess().getCommaKeyword_6());
		c.setNoLinewrap().before(
				g.getRealSequenceValueAccess().getCommaKeyword_6());
		c.setNoSpace().before(
				g.getBitStringTypeAccess().getCommaKeyword_3_2_0());
		c.setNoLinewrap().before(
				g.getBitStringTypeAccess().getCommaKeyword_3_2_0());
		c.setLinewrap().after(
				g.getSequenceTypeAccess().getComponentTypeListAssignment_3());
		c.setNoLinewrap().before(
				g.getAlternativeTypeListAccess().getCommaKeyword_1_0());
		c.setLinewrap().after(
				g.getAlternativeTypeListAccess().getCommaKeyword_1_0());
		c.setNoSpace().before(
				g.getExtensionAdditionAlternativesListAccess()
						.getCommaKeyword_1_0());
		c.setLinewrap().after(
				g.getExtensionAdditionAlternativesListAccess()
						.getCommaKeyword_1_0());
		c.setNoSpace().before(
				g.getAlternativeTypeListAccess().getCommaKeyword_1_0());
		c.setLinewrap().after(
				g.getAlternativeTypeListAccess().getCommaKeyword_1_0());

		// add Linebreaks before a SimpleValue
		c.setLinewrap(1, 1, 2).before(g.getSimpleValueAssignmentRule());
		// remove spaces and add Linebreaks and indentation for curled brackets
		// within sequences
		c.setNoSpace().before(
				g.getSequenceTypeAccess().getLeftCurlyBracketKeyword_2());
		c.setLinewrap().after(
				g.getSequenceTypeAccess().getLeftCurlyBracketKeyword_2());
		c.setIndentationIncrement().after(
				g.getSequenceTypeAccess().getLeftCurlyBracketKeyword_2());
		c.setIndentationDecrement().before(
				g.getSequenceTypeAccess().getRightCurlyBracketKeyword_6());
		c.setLinewrap().before(
				g.getSequenceTypeAccess().getRightCurlyBracketKeyword_6());
		// remove spaces around '<'
		c.setNoSpace().around(
				g.getLowerEndpointAccess().getClosedLessThanSignKeyword_1_0());
		c.setNoSpace().around(
				g.getUpperEndpointAccess().getClosedLessThanSignKeyword_0_0());
		// Remove spaces for Brackets
		List<Pair<Keyword, Keyword>> brackets = g.findKeywordPairs(
				OPENING_BRACKET.toString(), CLOSING_BRACKET.toString());
		for (Pair<Keyword, Keyword> pair : brackets) {
			c.setNoSpace().around(pair.getFirst());
			c.setNoSpace().before(pair.getSecond());
		}
		// Indentation for ChoiceType
		c.setNoLinewrap().after(
				g.getChoiceTypeAccess().getLeftCurlyBracketKeyword_2());
		c.setIndentationIncrement().before(
				g.getChoiceTypeAccess().getLeftCurlyBracketKeyword_2());
		c.setIndentationDecrement().after(
				g.getChoiceTypeAccess().getRightCurlyBracketKeyword_5());
		// Indentation for ElementSetSpecs
		c.setIndentationIncrement().before(g.getElementSetSpecsRule());
		c.setIndentationDecrement().after(g.getElementSetSpecsRule());
		// Indentation for Set
		c.setIndentationIncrement().after(
				g.getSetTypeAccess().getLeftCurlyBracketKeyword_2());
		c.setIndentationDecrement().after(
				g.getSetTypeAccess().getRightCurlyBracketKeyword_4());
		// Linebreak before UNION
		c.setLinewrap().before(g.getUnionsAccess().getUNIONKeyword_1_0_1());
		for (Keyword k : g.findKeywords(OPENING_CURLED_BRACKET.toString())) {
			c.setNoSpace().before(k);
		}
		// Linebreak after ',' within Sequence
		c.setLinewrap().after(
				g.getComponentTypeListAccess().getCommaKeyword_1_0());

		// Remove Linebreaks at the end of Sequence
		c.setNoLinewrap().before(
				g.getSequenceTypeAccess().getRightCurlyBracketKeyword_6());
		// Linebreaks before ComponentType
		c.setLinewrap().before(g.getComponentTypeRule());
		// Linebreak before a NamedType
		c.setLinewrap().before(g.getNamedTypeRule());
		// Remove Linebreaks for Intersection
		c.setNoLinewrap().before(g.getIntersectionElementsRule());
		c.setNoLinewrap().around(
				g.getValueRangeAccess().getFullStopFullStopKeyword_1());
		// Linebreaks brefore SequenceValue
		c.setLinewrap(1, 1, 2).before(g.getSequenceValueAssignmentRule());
		// remove Linebreaks before OPTION and DEFAULT
		c.setNoLinewrap().before(
				g.getNamedComponentTypeAccess().getOPTIONALKeyword_1_0());
		c.setNoLinewrap().before(
				g.getNamedComponentTypeAccess().getDEFAULTKeyword_1_1_0());
		// Linebreaks before NamedValues
		c.setLinewrap().before(g.getNamedValueRule());
		// Indentation and Linebreak for curled brackets within SequenceValue
		c.setLinewrap().after(
				g.getSequenceValueAccess().getLeftCurlyBracketKeyword_1());
		c.setIndentationIncrement().after(
				g.getSequenceValueAccess().getLeftCurlyBracketKeyword_1());
		c.setIndentationDecrement().before(
				g.getSequenceTypeAccess().getRightCurlyBracketKeyword_6());
		c.setLinewrap().before(
				g.getSequenceTypeAccess().getRightCurlyBracketKeyword_6());
		//

	}
}
